package com.orhon.smartcampus.modules.material.fetcher;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.gqlModel.GQLSchool;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.base.mm.SchoolMapper;
import com.orhon.smartcampus.modules.material.dao.RoomDao;
import com.orhon.smartcampus.modules.material.entity.Floor;
import com.orhon.smartcampus.modules.material.entity.Room;
import com.orhon.smartcampus.modules.material.gqlModel.GQLFloor;
import com.orhon.smartcampus.modules.material.gqlModel.GQLRoom;
import com.orhon.smartcampus.modules.material.mm.FloorMapper;
import com.orhon.smartcampus.modules.material.mm.RoomMapper;
import com.orhon.smartcampus.utils.ObjectToMap;
import com.orhon.smartcampus.utils.Util;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class RoomListFetcher implements DataFetcher  {
	@Autowired
	RoomDao roomDao;
	
	 @Override
	    public Object get(DataFetchingEnvironment env) throws Exception {
	    	 List<GQLRoom> GQLRoom = new ArrayList<>();
	         GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(env);
	         
	         String hql = "from Room where deleted_at = null ";

		 if(env.containsArgument("condition")) {
			 HashMap<String,Object> map = ObjectToMap.to(env.getArgument("condition"));
			 if(map.get("id")!=null) {
				 hql += "and id " + Util.decide(map.get("id"));
			 }
			 if(map.get("floor_id")!=null) {
				 hql += "and floor_id "+ Util.decide(map.get("floor_id"));
			 }
			 if(map.get("area")!=null) {
				 hql += "and  room_area " + Util.decide(map.get("area"));
			 }

			 if(map.get("code")!=null) {
				 hql += "and room_code " + Util.decide(map.get("code"));
			 }
			 if(map.get("type")!=null) {
				 hql += "and room_type " +  Util.decide(map.get("type"));
			 }
			 if(map.get("kye_num")!=null) {
				 hql += "and key_num " + Util.decide(map.get("kye_num"));
			 }
			 if(map.get("status")!=null) {
				 hql += "and room_status " + Util.decide(map.get("status"));
			 }
			 if(map.get("school_id")!=null) {
				 hql += "and school_id " + Util.decide(map.get("school_id"));
			 }
		 }

	         List<Room> list = roomDao.find(hql,null,page.getPage(),page.getLimit());
	         
	         for (Room item : list) {
	        	 GQLFloor gqlFloor = null;
	 			if (env.getSelectionSet().contains("data/floor")){
	 				Floor floor = item.getMaterialFloorByFloorId();
	 				gqlFloor = FloorMapper.MAPPER.toFloor(floor);
	 			}
	 			
	 			GQLSchool GgqlSchool = null;
	  			if (env.getSelectionSet().contains("data/school")){
	  				Schools schools = item.getBaseSchoolsBySchoolId();
	  				GgqlSchool = SchoolMapper.MAPPER.toSchool(schools);
	  			}

	 			GQLRoom room = RoomMapper.MAPPER.toRoom(item);
	 			room.setSchool(GgqlSchool);
	 			room.setFloor(gqlFloor);
	 			GQLRoom.add(room);
			}
	         
	         Long total = 0L;
	         if (env.getSelectionSet().contains("total")){
	         	total = roomDao.getTotalCount(hql);
	         }
	        return Result.ok().put("data", GQLRoom).put("total", total);
	    }
}
